package com.skillworld.webapp.model.tutorial;

import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.skillworld.webapp.model.tutorial.MarkedTutorial.Mark;
import com.skillworld.webapp.model.user.User;
import com.skillworld.webapp.model.util.dao.GenericDaoHibernate;

import es.udc.pojo.modelutil.exceptions.InstanceNotFoundException;



@Repository("MarkedTutorialDao")
public class MarkedTutorialDaoHibernate
        extends GenericDaoHibernate<MarkedTutorial,Long> 
        implements MarkedTutorialDao
{

    @SuppressWarnings("unchecked")
    public List<Mark> findTutorialMarks(Long userId, Long tutorialId)
    {
        return(List<Mark>) 
            getSession().createQuery(
                    "SELECT tm.mark FROM MarkedTutorial tm " +
                    "WHERE tm.user.userId = :usrId " +
                    "      AND tm.tutorial.tutorialId = :tutId")
                    .setParameter("usrId", userId)
                    .setParameter("tutId", tutorialId)
                    .list();
    }
    
    @SuppressWarnings("unchecked")
    public List<MarkedTutorial> findMarks(Long userId, Long tutorialId)
    {
        return(List<MarkedTutorial>) 
            getSession().createQuery(
                    "SELECT tm FROM MarkedTutorial tm " +
                    "WHERE tm.user.userId = :usrId " +
                    "      AND tm.tutorial.tutorialId = :tutId")
                    .setParameter("usrId", userId)
                    .setParameter("tutId", tutorialId)
                    .list();
    }
    
    public MarkedTutorial findMark(long userId, long tutorialId, Mark mark)
            throws InstanceNotFoundException
    {
        MarkedTutorial markedTutorial =(MarkedTutorial)
                getSession().createQuery("SELECT tm FROM MarkedTutorial tm " +
                                         "WHERE tm.user.userId = :usrId " +
                                         "  AND tm.tutorial.tutorialId = :tutId " +
                                         " AND tm.mark = :mark")
                                  .setParameter("usrId", userId)
                                  .setParameter("tutId", tutorialId)
                                  .setParameter("mark", mark)
                                  .uniqueResult();
        
        if (markedTutorial == null)
        { throw new InstanceNotFoundException(mark, MarkedTutorial.class.getName()); }
        
        return markedTutorial;
    }
    
    public boolean exists(long userId, long tutorialId, Mark mark)
    {
        return getSession().createQuery("SELECT tm FROM MarkedTutorial tm " +
                                        " WHERE tm.user.userId = :usrId " +
                                        "  AND tm.tutorial.tutorialId = :tutId " +
                                        "  AND tm.mark = :mark")
                    .setParameter("usrId", userId)
                    .setParameter("tutId", tutorialId)
                    .setParameter("mark", mark)
                    .uniqueResult() != null;
    }

    @Override
    public void removeMark(long userId, long tutorialId, Mark mark)
            throws InstanceNotFoundException 
    {
        MarkedTutorial markedTutorial = findMark(userId, tutorialId, mark);
        remove(markedTutorial.getMarkedId());
    }
}
